package com.jackrain.nea.omslocal.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.omslocal.conf.LoadCloumnsConfig;
import com.jackrain.nea.omslocal.util.SplitArrUtil;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.web.query.QueryEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * discription: 淘宝压测 查询并同步订单状态
 * <p>
 * Created with IntelliJ IDEA.
 *
 * @author cs
 * @date 2017/10/12 15:09
 */
@Component
@Slf4j
public class OrderStatusQueryService {

    private final SendMqServies sendMq;
    private final PropertiesConf pconf;
    private final ChangeOrdStatusService changeOrd;

    @Autowired
    private SplitArrUtil splitArrUtil;

    @Autowired
    public OrderStatusQueryService(SendMqServies sendMq, PropertiesConf pconf, ChangeOrdStatusService changeOrd) {
        this.sendMq = sendMq;
        this.pconf = pconf;
        this.changeOrd = changeOrd;
    }


    /**
     * 淘宝压测回传
     */
    public void ordStart() {
        // 获取自身 bean
        OrderStatusQueryService queryService = ApplicationContextHandle.getBean(OrderStatusQueryService.class);
        queryService.queryOrdStatus();
    }

    /**
     * 循环遍历查询订单状态表,以消息队列同步至线上
     */
    @Transactional(rollbackFor = Exception.class)
    public void queryOrdStatus() {

        String tag = this.pconf.getProperty("ons.ordstatusTag");
        String topic = this.pconf.getProperty("ons.ordtatusTopid", "TAOBAO_EVENTPRO");
        int rownums = pconf.getPropertyInt("ons.ordnum");
        int senNum = pconf.getPropertyInt("ons.pushorderNum");

        log.debug(Thread.currentThread().getName());
        try {
            // 需要查询的字段    t_omsorderlink
            String files = (String) LoadCloumnsConfig.ordstatusmap.get(pconf.getProperty("ons.ordstatustable"));
            // 查询订单状态表
//            String countSql = "select " + files + " from T_OMSORDERLINK where syncstatus = 0 and platform = 'MAIN' " +
//                    "and rownum <= " + rownums + " order by id for update skip locked";
            String countSql = "select " + files + " from T_OMSORDERLINK where syncstatus = 0 and platform = 'MAIN' " +
                    "and id <= (select max(id) from T_OMSORDERLINK) order by id for update skip locked";

            JSONArray jsonArr = QueryEngine.getInstance().doQueryObjectArray(countSql);

            if (jsonArr == null || jsonArr.isEmpty()) {
                log.debug("ordstatusArr is null");
                return;
            }

            if (jsonArr.size() > 200 ){
               JSONArray paramArr = splitArrUtil.splitArr(jsonArr,senNum);
                for (Object obj2 : paramArr){
                    // 拆分数据发送
                    JSONArray senArr = JSONArray.parseArray(obj2.toString());

                    JSONObject transeObj = new JSONObject();
                    JSONObject chekObj = new JSONObject();
                    JSONObject notifyObj = new JSONObject();
                    JSONObject outObj = new JSONObject();

                    transeObj.put("status", pconf.getProperty("ons.ordstatus1"));
                    chekObj.put("status", pconf.getProperty("ons.ordstatus2"));
                    notifyObj.put("status", pconf.getProperty("ons.ordstatus3"));
                    outObj.put("status", pconf.getProperty("ons.ordstatus4"));

                    JSONArray traseArr = new JSONArray();
                    JSONArray chekArr = new JSONArray();
                    JSONArray notifyArr = new JSONArray();
                    JSONArray outArr = new JSONArray();

                    // 循环遍历,数据分组
                    for (Object obj : senArr) {
                        JSONObject tempObj = JSONObject.parseObject(obj.toString());

                        if (tempObj.getString("STATUS").equals(pconf.getProperty("ons.ordstatus1"))) {
                            String tid = tempObj.getString("TID");
                            tempObj.fluentClear();
                            tempObj.put("tid", tid);
                            traseArr.add(tempObj);

                        } else if (tempObj.getString("STATUS").equals(pconf.getProperty("ons.ordstatus2"))) {
                            String tid = tempObj.getString("TID");
                            tempObj.fluentClear();
                            tempObj.put("tid", tid);
                            chekArr.add(tempObj);

                        } else if (tempObj.getString("STATUS").equals(pconf.getProperty("ons.ordstatus3"))) {
                            String tid = tempObj.getString("TID");
                            tempObj.fluentClear();
                            tempObj.put("tid", tid);
                            notifyArr.add(tempObj);

                        } else if (tempObj.getString("STATUS").equals(pconf.getProperty("ons.ordstatus4"))) {
                            String tid = tempObj.getString("TID");
                            tempObj.fluentClear();
                            tempObj.put("tid", tid);
                            outArr.add(tempObj);
                        }
                    }
                    log.debug("trans : " + traseArr.size());
                    log.debug("chek :" + chekArr.size());
                    log.debug("notify :" + notifyArr.size());
                    log.debug("out :" + outArr.size());
                    transeObj.put("bills", traseArr);
                    chekObj.put("bills", chekArr);
                    notifyObj.put("bills", notifyArr);
                    outObj.put("bills", outArr);

                    JSONArray dataArr = new JSONArray();
                    JSONObject jsonObject = new JSONObject();
                    dataArr.add(transeObj);
                    dataArr.add(chekObj);
                    dataArr.add(notifyObj);
                    dataArr.add(outObj);
                    jsonObject.put("data", dataArr);
                    // 有需要同步的订单数据 将查询到的数据以消息形式发送
                    sendMq.sendMqinfoObj(jsonObject, topic, tag);
                    //// 更改库存状态
                    changeOrd.changeStatus(senArr);

                }

            }else {
                JSONObject transeObj = new JSONObject();
                JSONObject chekObj = new JSONObject();
                JSONObject notifyObj = new JSONObject();
                JSONObject outObj = new JSONObject();

                transeObj.put("status", pconf.getProperty("ons.ordstatus1"));
                chekObj.put("status", pconf.getProperty("ons.ordstatus2"));
                notifyObj.put("status", pconf.getProperty("ons.ordstatus3"));
                outObj.put("status", pconf.getProperty("ons.ordstatus4"));

                JSONArray traseArr = new JSONArray();
                JSONArray chekArr = new JSONArray();
                JSONArray notifyArr = new JSONArray();
                JSONArray outArr = new JSONArray();

                // 循环遍历,数据分组
                for (Object obj : jsonArr) {
                    JSONObject tempObj = JSONObject.parseObject(obj.toString());
                    if (tempObj.getString("STATUS").equals(pconf.getProperty("ons.ordstatus1"))) {
                        String tid = tempObj.getString("TID");
                        tempObj.fluentClear();
                        tempObj.put("tid", tid);
                        traseArr.add(tempObj);

                    } else if (tempObj.getString("STATUS").equals(pconf.getProperty("ons.ordstatus2"))) {
                        String tid = tempObj.getString("TID");
                        tempObj.fluentClear();
                        tempObj.put("tid", tid);
                        chekArr.add(tempObj);

                    } else if (tempObj.getString("STATUS").equals(pconf.getProperty("ons.ordstatus3"))) {
                        String tid = tempObj.getString("TID");
                        tempObj.fluentClear();
                        tempObj.put("tid", tid);
                        notifyArr.add(tempObj);

                    } else if (tempObj.getString("STATUS").equals(pconf.getProperty("ons.ordstatus4"))) {
                        String tid = tempObj.getString("TID");
                        tempObj.fluentClear();
                        tempObj.put("tid", tid);
                        outArr.add(tempObj);
                    }
                }

                transeObj.put("bills", traseArr);
                chekObj.put("bills", chekArr);
                notifyObj.put("bills", notifyArr);
                outObj.put("bills", outArr);

                JSONArray dataArr = new JSONArray();
                JSONObject jsonObject = new JSONObject();
                dataArr.add(transeObj);
                dataArr.add(chekObj);
                dataArr.add(notifyObj);
                dataArr.add(outObj);
                jsonObject.put("data", dataArr);
                // 有需要同步的订单数据 将查询到的数据以消息形式发送
                sendMq.sendMqinfoObj(jsonObject, topic, tag);
                //// 更改库存状态
                changeOrd.changeStatus(jsonArr);
            }
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            // 将错误以流写出
            e.printStackTrace(pw);
            System.out.println(sw.toString());
        }


    }
}
